package com.mini.vo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.mini.model.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shixiaopeng on 2020/9/9
 */
@Data
public class BusinessTraditionChanceVO extends ChanceBaseInfoVO{

    @ApiModelProperty(value = "系统来源： 0 投放 2手工导入")
    private Integer systemFrom;

    @Excel(name = "系统来源：  投放 系统(手工导入)",orderNum = "8", needMerge = true)
    private String systemFromDesc;
    /**
     * 项目
     */
    @ApiModelProperty(value = "项目")
    @Excel(name = "项目", orderNum = "1", needMerge = true)
    private String itemName;

    @ApiModelProperty(value = "项目id")
    private Integer itemId;


    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Excel(name = "创建时间",format = "yyyy-MM-dd HH:mm:ss",orderNum = "11"  , width = 18, needMerge = true)
    private Date createTime;

    @ApiModelProperty(value = "未分配原因")
    @Excel(name = "未分配原因", orderNum = "5", needMerge = true)
    private String assignNotReason;

    @ApiModelProperty(value = "录入类型，0表单名片，1在线名片，2手动录入")
    private Integer enterType;

    @ApiModelProperty(value = "分配类型，0系统分配 1人工分配 2转归属")
    private Integer distributeType;
    /**
     * 分配时间
     */
    @ApiModelProperty(value = "分配时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Excel(name = "分配时间",format = "yyyy-MM-dd HH:mm:ss",orderNum = "11"  , width = 18, needMerge = true)
    private Date distributeTime;

    @ApiModelProperty(value = "是否可以手动分配")
    private Boolean handDistribute;

    @ApiModelProperty(value = "反馈时间(机会状态从未跟进变为跟进中的的时间)")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Excel(name = "反馈时间",format = "yyyy-MM-dd HH:mm:ss",orderNum = "11"  , width = 18, needMerge = true)
    private Date feedbackTime;

    /**
     * 末次跟进时间
     */
    @ApiModelProperty(value = "末次跟进时间")
    @Excel(name = "末次跟进时间",format = "yyyy-MM-dd HH:mm:ss",orderNum = "11"  , width = 18, needMerge = true)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastFollowUpTime;

    @ApiModelProperty(value = "末次有效跟进时间(发微信/打电话)")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Excel(name = "末次有效跟进时间",format = "yyyy-MM-dd HH:mm:ss",orderNum = "11"  , width = 18, needMerge = true)
    private Date lastValidFollowUpTime;

    @ApiModelProperty("军团ID")
    private Long skuId;
    @ApiModelProperty("军团名称")
    @Excel(name = "军团名称", orderNum = "5", needMerge = true)
    private String skuName;

    @ApiModelProperty("运营专员所属的量子军团ID")
    private Long teacherWorkRoomId;
    @ApiModelProperty("运营专员所属的量子军团名称")
    @Excel(name = "量子军团名称", orderNum = "5", needMerge = true)
    private String teacherWorkRoomName;

    @ApiModelProperty("销售部ID")
    private Long odId;
    @ApiModelProperty("销售部名称")
    @Excel(name = "销售部名称", orderNum = "5", needMerge = true)
    private String odName;

    @ApiModelProperty("销售组ID")
    private Long ogId;
    @ApiModelProperty("销售组名称")
    @Excel(name = "销售组名称", orderNum = "5", needMerge = true)
    private String ogName;

    /**
     *运营专员Id
     */
    @ApiModelProperty(value = "咨询师/运营专员Id")
    private Long teacherId;
    /**
     *运营专员名称
     */
    @ApiModelProperty(value = "咨询师/运营专员名称")
    @Excel(name = "运营专员", orderNum = "5", needMerge = true)
    private String teacherName;

    @ApiModelProperty(value = "投放工作室id", required = true)
    private Long workRoomId;

    @ApiModelProperty(value = "投放工作室名称", required = true)
//    @Excel(name = "投放工作室", orderNum = "18", needMerge = true)
    private String workRoomName;

    @ApiModelProperty(value = "负责人id/投放人员", required = true)
    private Long fzId;

    @ApiModelProperty(value = "负责人名称/投放人员", required = true)
    @Excel(name = "投放人员", orderNum = "19", needMerge = true)
    private String fzName;

    /**
     *飞天站点
     */
    @ApiModelProperty(value = "飞天站点")
    @Excel(name = "飞天站点", orderNum = "19", needMerge = true)
    private Long miniSiteId;
    /**
     *飞天站点名称
     */
    @ApiModelProperty(value = "飞天站点名称")
    @Excel(name = "飞天站点名称", orderNum = "20", needMerge = true)
    private String miniSiteName;

    @ApiModelProperty(value = "渠道id")
    private Integer channelId;

    @ApiModelProperty(value = "渠道标识")
    private String channelCode;

    @ApiModelProperty(value = "渠道名称")
    @Excel(name = "投放渠道", orderNum = "21", needMerge = true)
    private String channelName;

    @ApiModelProperty(value = "拨打次数（包括未打通）")
    @Excel(name = "拨打次数", orderNum = "6", needMerge = true)
    private Integer callNum;

    //今日拨打次数
    @ApiModelProperty(value = "今日拨打次数（包括未打通）")
    @Excel(name = "今日拨打次数", orderNum = "6", needMerge = true)
    private Integer todayCallNum;

    //今日微信条数
    @ApiModelProperty(value = "今日微信条数")
    @Excel(name = "今日微信条数", orderNum = "6", needMerge = true)
    private Integer todayChatNum;

    @ApiModelProperty(value = "备注信息-最后一条")
    BusinessChanceCommentRecord commentLasted;
    @ApiModelProperty(value = "用户画像")
    Map<String, BusinessChanceUserProfile> userProfile;

    @ApiModelProperty(value = "加微状态")
    private Boolean isAdd = false;
    @Excel(name = "加微状态", orderNum = "6", needMerge = true)
    private String isAddDesc;

    @ApiModelProperty(value = "是否拨打电话")
    private Boolean isCall = false;
    @Excel(name = "是否拨打电话", orderNum = "6", needMerge = true)
    private String isCallDesc;

    @ApiModelProperty(value = "报名信息")
//    @ExcelEntity(name = "报名信息")
    private BusinessBigLessonOrderVO bigLessonOrderVO;

    /**
     * 手机号
     */
    @ApiModelProperty(value = "手机号集合")
    @ExcelCollection(name = "手机号集合", orderNum = "30")
    private List<BusinessChancePhone> phoneList;
    /**
     * 微信号
     */
    @ApiModelProperty(value = "微信号集合")
    @ExcelCollection(name = "微信号集合", orderNum = "30")
    private List<BusinessChanceWechat> wechatList;


    public static BusinessTraditionChanceVO fromDO(BusinessTraditionChance businessTraditionChance) {
        if (Objects.isNull(businessTraditionChance)) {
            return null;
        }
        BusinessTraditionChanceVO businessTraditionChanceVO = new BusinessTraditionChanceVO();
        BeanUtils.copyProperties(businessTraditionChance, businessTraditionChanceVO);
        List<BusinessChancePhone> phoneList = businessTraditionChanceVO.getPhoneList();
        if(CollectionUtils.isNotEmpty(phoneList)){
            boolean match = phoneList.stream().anyMatch(businessChancePhone -> Objects.nonNull(businessChancePhone.getCallNum()) && businessChancePhone.getCallNum() > 0);
            businessTraditionChanceVO.setIsCall(match);
        }
        List<BusinessChanceWechat> wechatList = businessTraditionChanceVO.getWechatList();
        if(CollectionUtils.isNotEmpty(wechatList)){
            boolean match = wechatList.stream().anyMatch(businessChanceWechat -> businessChanceWechat.getIsAdd()==null? false : businessChanceWechat.getIsAdd());
            businessTraditionChanceVO.setIsAdd(match);
        }
        return businessTraditionChanceVO;
    }

    public static List<BusinessTraditionChanceVO> fromDO(List<BusinessTraditionChance> businessTraditionChances) {
        if (Objects.isNull(businessTraditionChances)) {
            return Collections.emptyList();
        }

        return businessTraditionChances.stream()
                .map(BusinessTraditionChanceVO::fromDO)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }


    public enum DistributeTypeEnum{
        //分配类型，0系统分配 1人工分配 2转归属
        convert(2,"转归属"),
        handDistribute(1, "人工手动分配"),
        systemDistribute(0,"系统分配"),
        ;
        public final Integer code;
        public final String desc;

        DistributeTypeEnum(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

    }

}
